import { Command, CommanderStatic } from 'commander';
import { IAction, LinkAction } from '../../actions';
import { CommandBase } from '../command-base/command-base';
import { Input } from '../../core';

export class LinkCommand extends CommandBase {
  /**
   * 加载命令
   *
   * @author chitanda
   * @date 2021-04-18 13:04:43
   * @abstract
   * @param {CommanderStatic} program
   */
  protected load(program: CommanderStatic): void {
    program
      .command('link')
      .arguments('<source> <target>')
      .description('建立软链接', {
        source: '需要链接的「文件、文件夹」',
        target: '链接至的目标目录',
      })
      .usage('<source> <target>')
      .option('-f, --force', '当指向的目标文件存在时，删除原有[文件 or 文件夹]并建立软链接!')
      .action(async (source: string, target: string, options: Record<string, string | boolean>, _command: Command) => {
        const input: Input = {};
        input.source = { name: 'source', value: source };
        input.target = { name: 'target', value: target };
        const opts: Input = {};
        opts.force = { name: 'force', value: options.force };
        try {
          await this.action.handle(input, opts);
        } catch (err) {
          process.exit(0);
        }
      });
  }

  /**
   * 生成行为
   *
   * @author chitanda
   * @date 2021-04-18 13:04:04
   * @protected
   * @abstract
   * @return {*}  {IAction}
   */
  protected getAction(): IAction {
    return new LinkAction();
  }
}
